package com.csun.nusing.ui.familyactivity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.BaseJsonBean;
import com.csun.nusing.bean.jsonbean.FamiltUnBindDeviceBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.L;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.ToastUtil;
import com.csun.zxing.CaptureActivity;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人绑定空余设备界面
 * 可以添加设备进行绑定
 * 点击老人详情的绑定设备按钮--跳转到该界面
 * 作者: Created by fzq on 2018/2/12 20:25
 * 邮箱: 15997123593@163.com
 */
public class FamilyBindDeviceListActivity extends BaseActivity<CommonPresenter>{

    private String olderId;
    private ListView lv;
    private ImageView addiv;
    private String authorization;
    private FamiltUnBindDeviceBean devicejsonbean;
    private ImageView iconiv;
    private TextView codetv;
    private TextView nametv;
    private TextView timetv;
    private String url;
    private String devicetype;
    private String devicecode;
    private TextView addtv;
    private TextView emptytv;

    @Override
    public int getContentViewId() {
        return R.layout.activity_family_bind_deviceist;
    }

    @Override
    public void initSubViews(View view) {
        super.initSubViews(view);

        lv = (ListView) findViewById(R.id.activity_alldevice_list);
        addiv = (ImageView) findViewById(R.id.family_devicelist_add_iv);
        addtv = (TextView) findViewById(R.id.family_adddevice_add_tv);
        emptytv = (TextView) findViewById(R.id.activity_alldevice_list_empty_tv);
        lv.setEmptyView(emptytv);
        //扫码添加
        addtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加设备绑定
//                扫码
                Intent intent = new Intent(FamilyBindDeviceListActivity.this, CaptureActivity.class);
                intent.putExtra("isFamilyBindList",true);
                startActivityForResult(intent,0x11);
            }
        });
        addiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加设备绑定
//                扫码
                Intent intent = new Intent(FamilyBindDeviceListActivity.this, CaptureActivity.class);
                intent.putExtra("isFamilyBindList",true);
                startActivityForResult(intent,0x11);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
        url = data.getStringExtra("url");
        //添加设备
        addDevice();
        }

    }

    /**
     * 添加设备
     *
     * @Description: TODO
     * @param
     * @return void
     * @throws
     * @author fzq
     * @date 2017-11-17
     */
    protected void addDevice() {
        if (url != null) {
            String[] urlss = url.split(",");
            if (urlss != null && urlss.length > 1) {
                devicetype = urlss[0];
                devicecode = urlss[1];
            }
        }
        if (devicetype.equals("0")) {
            addFollow();
        } else if (devicetype.equals("1")) {
            addWatch();
        } else if (devicetype.equals("2")) {
            addBed();
        } else if (devicetype.equals("3")) {
            addAlert();
        }
    }

    private void addAlert() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("deviceNo", devicecode + "");
        param.put("deviceType", devicetype + "");
        param.put("deviceInfo",  "");
        param.put("deviceMac", "");
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            BaseJsonBean baseJsonBean = gson.fromJson(result,
                                    BaseJsonBean.class);
                            if (baseJsonBean.isStatus()) {
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                                info();
                            }else{
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                            }
                        }
                    }
                }).start(UrlBean.ADDALERTURL);
    }

    private void addBed() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("deviceNo", devicecode + "");
        param.put("deviceType", devicetype + "");
        param.put("deviceInfo", "");
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            BaseJsonBean baseJsonBean = gson.fromJson(result,
                                    BaseJsonBean.class);
                            if (baseJsonBean.isStatus()) {
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                                info();
                            }else{
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                            }
                        }
                    }
                }).start(UrlBean.ADDSLEEPURL);
    }

    private void addWatch() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("deviceNo", devicecode + "");
        param.put("deviceType", devicetype + "");
        param.put("deviceInfo", "");
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            BaseJsonBean baseJsonBean = gson.fromJson(result,
                                    BaseJsonBean.class);
                            if (baseJsonBean.isStatus()) {
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                                info();
                            }else{
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                            }
                        }
                    }
                }).start(UrlBean.ADDWATCHURL);
    }

    /**
     * 添加随访仪
     *
     * @Description: TODO
     * @param
     * @return void
     * @throws
     * @author fzq
     * @date 2017-11-17
     */
    private void addFollow() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("deviceNo", devicecode + "");
        param.put("deviceType", devicetype + "");
        param.put("deviceInfo", "");
        param.put("androidMac", "");
        param.put("iosMac", "");
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            BaseJsonBean baseJsonBean = gson.fromJson(result,
                                    BaseJsonBean.class);
                            if (baseJsonBean.isStatus()) {
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                                info();
                            }else{
                                ToastUtil.showMessage(getApplicationContext(),
                                        baseJsonBean.getMessage());
                            }
                        }
                    }
                }).start(UrlBean.ADDFOLLOWURL);
    }



    /**
     * 添加成功则刷新设备列表
     */
    private void info() {
        ToastUtil.showMessage(this,R.string.add_successful);
        requestListData();
    }


    @Override
    public void initData() {
        super.initData();
        olderId = getIntent().getStringExtra("olderId");
        authorization = (String) SPUtils.get(this, "authorization", "");
        requestListData();

    }

    /**
     * 加载空余设备列表
     * @Description: TODO
     * @param @param list
     * @return void
     * @throws
     * @author fzq
     * @date 2017-11-3
     */
    private void requestListData() {
        Map<String, String> param = new HashMap<String, String>();
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            L.e("fzq", "-------" + result);
                            Gson gson = new Gson();
                            devicejsonbean = gson.fromJson(result,
                                    FamiltUnBindDeviceBean.class);
                            if (devicejsonbean != null) {
                                if (devicejsonbean.getData() != null) {
                                    controlfollowList(devicejsonbean.getData());
                                }
                            }
                        }
                    }
                }).start(UrlBean.nusingselectfamilyURL);
    }

    /**
     * 设备列表
     * @param data
     */
    private void controlfollowList(final List<FamiltUnBindDeviceBean.FamiltUnBindDeviceDataBean> data) {

        final MyBaseAdapter<FamiltUnBindDeviceBean.FamiltUnBindDeviceDataBean> myBaseAdapter = new MyBaseAdapter<FamiltUnBindDeviceBean.FamiltUnBindDeviceDataBean>(
                this, data, R.layout.item_device_list) {
            @Override
            public void convert(ViewHolder helper, int position,
                                FamiltUnBindDeviceBean.FamiltUnBindDeviceDataBean item) {
                iconiv = (ImageView) helper
                        .getView(R.id.item_device_list_device_icon);
                codetv = (TextView) helper
                        .getView(R.id.item_device_list_device_code_tv);
                nametv = (TextView) helper
                        .getView(R.id.item_device_list_device_name_tv);
                timetv = (TextView) helper
                        .getView(R.id.item_device_list_device_time_tv);
                        // 床垫
                iconiv.setImageResource(R.drawable.shebei_icon_chuangdian_js);
                codetv.setText(getString(R.string.device_sleep)+"      " + (item.getDeviceNo()!=null?item.getDeviceNo():"-"));

            }
        };
        lv.setAdapter(myBaseAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //绑定床垫
                bindOlderid(data.get(i).getId());
            }
        });

    }


    /**
     * 绑定老人与设备
     * @param deviceId
     */
    private void bindOlderid(String deviceId) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("oldmanid", ""+olderId);
        param.put("mattressid", ""+deviceId);
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            L.e("FZQ", "---" + result);
                            Gson gson = new Gson();
                            BaseJsonBean bean = gson.fromJson(result,BaseJsonBean.class);
                            if (bean.isStatus()){
                                ToastUtil.showMessage(getApplicationContext(),R.string.bang_ding_successful);
                                finish();
                            }
                        }
                    }
                }).start(UrlBean.BEDBINDOLDERURL);
    }



}
